package com.xhga.transaction.common.annotation.factory;

import com.xhga.transaction.common.annotation.XHGAProxy;
import com.xhga.transaction.common.jdbc.TransactionLogJDBCService;
import com.xhga.transaction.common.redis.RedisApi;
import com.xhga.transaction.common.service.TransactionLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;

import java.lang.reflect.Proxy;

import static com.xhga.transaction.common.util.XHGAConfig.PROXY_JDBC;

/**
 * 获得动态代理对象
 * Created by HuaWeiBo on 2019/4/23.
 */
@Component(value = PROXY_JDBC)
public class XHGAJDBCProxyFactory<T> implements ProxyFactory<T>{

    @Autowired
    private PlatformTransactionManager transactionManager;

    @Autowired
    private RedisApi redisApi;

    @Override
    public T getProxy(Class api, T service){
        TransactionLogService transactionLogService = new TransactionLogJDBCService(redisApi);
        XHGAProxy<Object> proxy = new XHGAProxy<>(transactionManager, transactionLogService, service);
        return (T) Proxy.newProxyInstance(api.getClassLoader(),
                new Class[]{api}, proxy);
    }

}
